package com.llf.Controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Pojo.Bill;
import com.llf.Pojo.Provider;
import com.llf.Service.BillServiceImpl;
import com.llf.Service.ProviderServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class BillController {
    @Autowired
    private BillServiceImpl billService;
    @Autowired
    private ProviderServiceImpl providerService;
    @RequestMapping("/toBilllist")
    public String toBilllist(Model model, String productName, Integer providerId,Integer isPayment,
                              Integer pageIndex,Integer pageSize)
    {

        if (providerId==null){
            providerId=0;
        }
        if (isPayment==null){
            isPayment=0;
        }
        // 为了程序的严谨性，判断非空：
        if (pageIndex == null) {
            pageIndex = 1; // 设置默认当前页
        }
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6; // 设置默认每页显示的数据数
        }
        ArrayList<Provider> providerList = providerService.getProName();
        PageHelper.startPage(pageIndex, pageSize);
        List<Bill> page = billService.getBillList(productName, providerId,isPayment);
        // 使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(page);
        // 清理 ThreadLocal 存储的分页参数,保证线程安全
        PageHelper.clearPage();
        model.addAttribute("BillList",page);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("providerList",providerList);
        return "billlist";
    }
    @GetMapping("/toBsee")
    public String toBsee(Integer id, Model model){
        Bill bill = billService.getBillById(id);
        model.addAttribute("bill",bill);
        return "billview";
    }
    @GetMapping("/toBupdate")
    public String toBupdate(Integer id,Model model){
        Bill bill = billService.getBillById(id);
        ArrayList<Provider> proNamelist = providerService.getProName();
        model.addAttribute("bill",bill);
        model.addAttribute("proNamelist",proNamelist);
        return "billmodify";
    }

    @PostMapping("/Bupdate")
    public String Bupdate(Bill bill){
        billService.updateBill(bill);
        return "redirect:/toBilllist";
    }
    @GetMapping("/toBdelete")
    public String Bdelete(Integer id){
        billService.deleteBillById(id);
        return "redirect:/toBilllist";

    }
    @GetMapping("/toBadd")
    public String toBadd(Model model){
        ArrayList<Provider> proNamelist = providerService.getProName();
        model.addAttribute("proNamelist",proNamelist);
        return "billadd";
    }
    @PostMapping("/Badd")
    public String Badd(Bill bill){
        billService.addBill(bill);
        return "redirect:/toBilllist";
    }
    @GetMapping("BdeleteB")
    @ResponseBody
    public String delete(Integer id){
        Bill bill = billService.getBillById(id);
        HashMap<String, String> map = new HashMap<>();
        if (bill!=null){
            map.put("delResult","删除成功");
        }else {
            map.put("delResult","删除失败");
        }
        String jsonString = JSON.toJSONString(map);
        return  jsonString;
    }
}
